#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int datatype;

typedef struct SListNode
{
	datatype data;
	struct SListNode* next;
	struct SListNode* prev;
}DLN;

DLN* SListInit()
{
	DLN* phead = (DLN*)malloc(sizeof(DLN));
	if (phead == NULL)
	{
		printf("malloc fail\n");
		return NULL;
	}
	phead->prev = phead->next = phead;
	return phead;
}
void DListInsert_head(DLN* phead,datatype x)
{
	DLN* first = phead->next;
	DLN* newnode = (DLN*)malloc(sizeof(DLN));
	if (newnode == NULL)
	{
		printf("malloc fail");
		return;
	}
	newnode->data = x;
	newnode->prev = NULL;
	newnode->next = NULL;
	
	phead->next = newnode;
	newnode->prev = phead;
	first->prev = newnode;
	newnode->next = first;
}

void DListInsert_tail(DLN* phead, datatype x)
{
	DLN* tail = phead->prev;
	DLN* newnode = (DLN*)malloc(sizeof(DLN));
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	newnode->prev = NULL;

	tail->next = newnode;
	newnode->prev = tail;
	phead->prev = newnode;
	newnode->next = phead;
}
bool is_empty(DLN* phead)
{
	if (phead->prev == phead && phead->next == phead)
		return true;
	else
		return false;
}
void Dbleprint(DLN* phead)
{
	if (is_empty(phead))
		return;
	DLN* cur = phead->next;
	while (cur != phead)
	{
		printf("%d ", cur->data);
		cur = cur->next;
	}
	printf("\n");
}
int main()
{
	DLN* plist = SListInit();
	DListInsert_head(plist, 0);
	DListInsert_head(plist, 1);
	DListInsert_head(plist, 2);
	DListInsert_head(plist, 3);
	Dbleprint(plist);
	DListInsert_tail(plist, -1);
	Dbleprint(plist);
	return 0;
}